package com.dayaokeji.rhythmschoolstudent.client.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.utils.AndroidUtils;
import com.dayaokeji.rhythmschoolstudent.utils.InputUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.SystemApi;
import com.dayaokeji.server_api.domain.Feedback;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 意见反馈
 */
public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_contact)
    EditText etContact;

    private static final SystemApi systemApi = ApiUtils.getApi(SystemApi.class);
    private Call<ServerResponse<Void>> feedbackCall;

    public static void navigation(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        setSupportActionBar(toolbar);
    }

    @OnClick({R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                sendValidate();
                break;
        }
    }

    /**
     * 发送反馈之前的验证
     */
    private void sendValidate() {
        if (InputUtils.validateInput(etContact,"请输入您的联系方式") && InputUtils.validateInput(etContent,"请输入您要反馈的内容") ) {
            send();
        }
    }

    /**
     * 发送反馈
     */
    private void send() {
        Feedback feedback = new Feedback();
        feedback.setUserId(UserUtils.getUserInfo().getId());
        feedback.setContact(etContact.getText().toString().trim());
        feedback.setRetroaction(etContent.getText().toString().trim());
        feedback.setVersion(AndroidUtils.getVersionName());
        feedback.setPhoneModels(Build.MODEL);
        feedback.setType(1);

        feedbackCall = systemApi.feedback(feedback);
        feedbackCall.enqueue(new ServerCallback<Void>(this,"正在发送...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    ToastUtils.success(getString(R.string.send_success));
                    ActivityStackManager.finish();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (feedbackCall != null) {
            feedbackCall.cancel();
        }
        super.onDestroy();
    }
}
